﻿using System;
using System.Collections.Generic;
using System.Windows;
using Kiva7.Controls;
using Kiva7.DataClasses;
using Kiva7.DataRequests;
using Kiva7.Images;
using Kiva7.Translations;
using Kiva7.Utilities;
using Microsoft.Phone.Controls;

namespace Kiva7.Pages
{
    public partial class TeamDetailPage
    {
        private const string Url = "/Pages/TeamDetailPage.xaml";
        public static readonly Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        public static Uri GetPageUrl(IEnumerable<Team> teamList,
                                     int index)
        {
            if (teamList == null)
            {
                return null;
            }

            List<int> list = new List<int>();
            foreach (Team team in teamList)
            {
                list.Add(team.TeamId);
            }

            UrlCreator url = new UrlCreator(Url);
            url.Add(CacheGuidKey, CacheIdList(list));
            url.Add(IndexKey, index);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }

        public TeamDetailPage()
        {
            InitializeComponent();
            CreateStandardControls();
            SetPivot(Pivot);
        }


        private void PhoneApplicationPageLoaded(object sender,
                                                RoutedEventArgs e)
        {
            _request = new TeamDetailRequest();
            DataContext = _request;

            _teamLendersRequest = new TeamLendersRequest();
            LendersControl.DataContext = _teamLendersRequest;

            RequestMain();

            ApplicationBar.IsVisible = IdList.Count > 1;

            CheckPivot(Pivot.SelectedItem as PivotItem);
        }


        private TeamDetailRequest _request;

        protected override void RequestMain()
        {
            if (_request.LoadStatus ==
                LoadStatus.NotLoaded)
            {
                _request.GetTeam(IdList[Index]);
            }
        }


        private TeamLendersRequest _teamLendersRequest;

        private void RequestLenders()
        {
            if (_teamLendersRequest.LoadStatus ==
                LoadStatus.NotLoaded)
            {
                _teamLendersRequest.GetLenders(IdList[Index], SortBy.Newest);
            }
        }

        protected override void CheckPivot(PivotItem pi)
        {
            if (_BarButtonTranslate != null)
            {
                _BarButtonTranslate.IsEnabled = pi == pvTeam;
            }

            if (pi == pvLenders)
            {
                RequestLenders();
            }
        }

        protected override void ClearRequests()
        {
            _request.Clear();
            _teamLendersRequest.Clear();
        }

        private void PivotLoadingPivotItem(object sender,
                                           PivotItemEventArgs e)
        {
            CheckPivot(e.Item);
        }

        private BindableApplicationBarButton _BarButtonTranslate;

        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.Back, ImageUrls.Back, BackClick);

            _BarButtonTranslate = AddBarButton(AppResources.TranslateTexts, ImageUrls.Translation, TranslateClick);

            AddBarButton(AppResources.Next, ImageUrls.Next, NextClick);
        }

        private void TranslateClick(object sender, EventArgs e)
        {
            TeamDetailControl.TranslateTextWithDetect();
        }

    }
}